using MicroMsg.Common.Utils;
using System;
using System.Collections.Generic;
using System.Windows;
namespace MicroMsg.Common.Event
{
	public class EventCenter
	{
		private const string TAG = "EventCenter";
		private static object lockEventMap = new object();
		private static Dictionary<EventConst, EventObject> mapEventObject = new Dictionary<EventConst, EventObject>();
		public static bool registerEventWatcher(EventConst eventID, EventWatcher eventWatcher)
		{
			if (eventWatcher != null && eventWatcher.mHandler != null)
			{
				bool result;
				lock (EventCenter.lockEventMap)
				{
					EventObject eventObject;
					if (EventCenter.mapEventObject.ContainsKey(eventID))
					{
						eventObject = EventCenter.mapEventObject.get_Item(eventID);
					}
					else
					{
						eventObject = new EventObject(eventID);
						EventCenter.mapEventObject.Add(eventID, eventObject);
					}
					result = eventObject.addWatcher(eventWatcher);
				}
				return result;
			}
			Log.smethod_1("EventCenter", "register event id without watcher, ignored.  eventid = " + eventID);
			return false;
		}
		public static bool removeEventWatcher(EventConst eventID, EventWatcher watcher)
		{
			bool result;
			lock (EventCenter.lockEventMap)
			{
				if (watcher != null && EventCenter.mapEventObject.ContainsKey(eventID))
				{
					EventObject eventObject = EventCenter.mapEventObject.get_Item(eventID);
					bool flag2 = eventObject.removeWatcher(watcher);
					if (eventObject.getCount() == 0)
					{
						EventCenter.mapEventObject.Remove(eventID);
					}
					result = flag2;
				}
				else
				{
					Log.smethod_1("EventCenter", "remove event watcher failed,  eventid= " + eventID);
					result = false;
				}
			}
			return result;
		}
		public static bool registerEventHandler(EventConst eventID, EventHandlerDelegate eventHandler)
		{
			EventWatcher eventWatcher = new EventWatcher(null, null, eventHandler);
			return EventCenter.registerEventWatcher(eventID, eventWatcher);
		}
		public static bool removeEventHandler(EventConst eventID, EventHandlerDelegate eventHandler)
		{
			bool result;
			lock (EventCenter.lockEventMap)
			{
				if (eventHandler != null && EventCenter.mapEventObject.ContainsKey(eventID))
				{
					EventObject eventObject = EventCenter.mapEventObject.get_Item(eventID);
					bool flag2 = eventObject.removeWatcher(eventHandler);
					if (eventObject.getCount() == 0)
					{
						EventCenter.mapEventObject.Remove(eventID);
					}
					result = flag2;
				}
				else
				{
					Log.smethod_1("EventCenter", "remove event handler failed,  eventid= " + eventID);
					result = false;
				}
			}
			return result;
		}
		public static bool registerEventWatcher(EventConst eventID, string watchName, EventHandlerDelegate eventHandler)
		{
			EventWatcher eventWatcher = new EventWatcher(watchName, eventHandler);
			return EventCenter.registerEventWatcher(eventID, eventWatcher);
		}
		public static bool removeEventWatcher(EventConst eventID, string watchName)
		{
			bool result;
			lock (EventCenter.lockEventMap)
			{
				if (!EventCenter.mapEventObject.ContainsKey(eventID))
				{
					Log.smethod_1("EventCenter", "remove event handler failed,  eventid= " + eventID);
					result = false;
				}
				else
				{
					EventObject eventObject = EventCenter.mapEventObject.get_Item(eventID);
					bool flag2 = eventObject.removeWatcher(watchName);
					if (eventObject.getCount() == 0)
					{
						EventCenter.mapEventObject.Remove(eventID);
					}
					result = flag2;
				}
			}
			return result;
		}
		public static void printfInfo()
		{
			lock (EventCenter.lockEventMap)
			{
				using (Dictionary<EventConst, EventObject>.Enumerator enumerator = EventCenter.mapEventObject.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						KeyValuePair<EventConst, EventObject> current = enumerator.get_Current();
						EventConst arg_2D_0 = current.get_Key();
						EventObject value = current.get_Value();
						if (value != null)
						{
							value.printInfo();
						}
					}
				}
			}
		}
		public static bool postEvent(EventConst eventID, object param1 = null, object param2 = null)
		{
			bool result;
			lock (EventCenter.lockEventMap)
			{
				if (!EventCenter.mapEventObject.ContainsKey(eventID))
				{
					result = false;
				}
				else
				{
					EventObject eventObject = EventCenter.mapEventObject.get_Item(eventID);
					result = eventObject.beginInvoke(new BaseEventArgs(eventID, param1, param2));
				}
			}
			return result;
		}
		public static bool postCombineEvent(EventConst eventID, object object_0)
		{
			bool result;
			lock (EventCenter.lockEventMap)
			{
				if (!EventCenter.mapEventObject.ContainsKey(eventID))
				{
					result = false;
				}
				else
				{
					EventObject eventObject = EventCenter.mapEventObject.get_Item(eventID);
					if (eventObject.args == null)
					{
						eventObject.args = new BaseEventArgs(eventID, new List<object>());
						Deployment.get_Current().get_Dispatcher().BeginInvoke(new DispatcherEventDelegate(EventCenter.onDispatchEvent), new object[]
						{
							eventObject
						});
					}
					((List<object>)eventObject.args.mObject).Add(object_0);
					result = true;
				}
			}
			return result;
		}
		public static bool sendEvent(EventConst eventID, object object_0 = null)
		{
			if (!DebugEx.IsMainThread())
			{
				return false;
			}
			List<EventWatcher> list = null;
			lock (EventCenter.lockEventMap)
			{
				if (!EventCenter.mapEventObject.ContainsKey(eventID))
				{
					Log.smethod_4("EventCenter", "post event = " + eventID + ", no watcher.");
					return false;
				}
				EventObject eventObject = EventCenter.mapEventObject.get_Item(eventID);
				list = new List<EventWatcher>(eventObject.listEventWatcher);
			}
			BaseEventArgs evtArgs = new BaseEventArgs(eventID, object_0);
			using (List<EventWatcher>.Enumerator enumerator = list.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					EventWatcher current = enumerator.get_Current();
					current.doInvoke(evtArgs);
				}
			}
			return true;
		}
		private static void onDispatchEvent(EventObject evtObject)
		{
			BaseEventArgs evtArgs = null;
			List<EventWatcher> list = null;
			lock (EventCenter.lockEventMap)
			{
				evtArgs = evtObject.args;
				evtObject.args = null;
				list = new List<EventWatcher>(evtObject.listEventWatcher);
			}
			using (List<EventWatcher>.Enumerator enumerator = list.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					EventWatcher current = enumerator.get_Current();
					current.doInvoke(evtArgs);
				}
			}
		}
	}
}
